﻿using System.Text;
using System.Net.Mail;
using System.Net;

namespace Public
{
    public static class EmailHelper
    {
        public static void SendEmail(string title, string body)
        {
            try
            {
                string Smtp = ConfigHelper.GetSetting("Smtp");
                string SendEmail = ConfigHelper.GetSetting("SendEmail");
                string SendEmailPwd = ConfigHelper.GetSetting("SendEmailPwd");
                string ReceiveEmail = ConfigHelper.GetSetting("ReceiveEmail");
                SmtpClient client = new SmtpClient(Smtp);   //设置邮件协议
                client.UseDefaultCredentials = false;//这一句得写前面
                client.DeliveryMethod = SmtpDeliveryMethod.Network; //通过网络发送到Smtp服务器
                client.Credentials = new NetworkCredential(SendEmail, SendEmailPwd); //通过用户名和密码 认证
                MailMessage mmsg = new MailMessage(SendEmail, ReceiveEmail); //发件人和收件人的邮箱地址
                mmsg.Subject = title;      //主题
                mmsg.SubjectEncoding = Encoding.UTF8;   //主题编码
                mmsg.Body = body;         //正文
                mmsg.BodyEncoding = Encoding.UTF8;      //正文编码
                mmsg.IsBodyHtml = true;    //设置为HTML格式       
                mmsg.Priority = MailPriority.Normal;   //优先级
                client.Send(mmsg);
            }
            catch (System.Exception ex)
            {
                MB.Util.TraceEx.Write("发送邮件失败：" + ex.Message);
            }
        }
    }
}
